﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BinhSon.Dao;

namespace BinhSon.Services.Impl
{
    public partial class BinhSonServices
    {
        #region public methods
        public void ThemHocSinh(HocSinh hs)
        {
            if (this.CheckExistHocSinh(hs.LopHocId, hs.SoThuTu))
            {
                var hocSinhOld =
                    this._bsEntities.HocSinh.SingleOrDefault(x => x.LopHocId == hs.LopHocId && x.SoThuTu == hs.SoThuTu);
                if (hocSinhOld != null)
                {
                    hocSinhOld.HoVaChuLot = hs.HoVaChuLot;
                    hocSinhOld.Ten = hs.Ten;
                    hocSinhOld.NgaySinh = hs.NgaySinh;
                    hocSinhOld.NoiSinh = hs.NoiSinh;
                    hocSinhOld.GioiTinh = hs.GioiTinh;                    
                    hocSinhOld.TenDayDu = hs.TenDayDu;
                    this._bsEntities.SaveChanges();    
                }                
            }
            else
            {
                this._bsEntities.HocSinh.Add(hs);
                this._bsEntities.SaveChanges();    
            }            
        }

        public bool ImportHocSinhFromDataSet(DataSet ds)
        {
            foreach (DataTable table in ds.Tables)
            {
                var currentLopHoc = 0;
                foreach (DataRow row in table.Rows)
                {
                    var itemArray = row.ItemArray;
                    var tenLop = itemArray[0].ToString();
                    var lopHoc = this.GetLopHocByTenLop(tenLop);
                    
                    //khong co lop hoc thi dung lai
                    if (lopHoc == null)
                    {
                        continue;
                    }

                    var lopHocId = lopHoc.LopHocId;                    

                    currentLopHoc = lopHocId;
                    var soThuTu = Convert.ToInt32(itemArray[1]);

                    var hocSinhOld = this.GetHocSinhByLopHocAndSoThuTu(lopHocId, soThuTu);                        
                    //co hoc sinh cu roi, update hoc sinh cu
                    if (hocSinhOld != null)
                    {
                        hocSinhOld.HoVaChuLot = itemArray[2].ToString();
                        hocSinhOld.Ten = itemArray[3].ToString();
                        hocSinhOld.TenDayDu = hocSinhOld.HoVaChuLot + " " + hocSinhOld.Ten;
                        hocSinhOld.GioiTinh = itemArray[4].ToString().ToLower() == "nam";
                        hocSinhOld.NgaySinh = (DateTime)itemArray[5];
                        hocSinhOld.NoiSinh = itemArray[6].ToString();                        
                        this._bsEntities.SaveChanges();
                    }
                    else
                    {
                        var ngaysinh = DateTime.Parse(itemArray[5].ToString());
                        var entity = new HocSinh
                        {
                            HoVaChuLot = itemArray[2].ToString(),
                            Ten = itemArray[3].ToString(),
                            NgaySinh = ngaysinh,
                            NoiSinh = itemArray[6].ToString(),                            
                            LopHocId = lopHocId,
                            SoThuTu = soThuTu
                        };
                        entity.TenDayDu = entity.HoVaChuLot + " " + entity.Ten;
                        entity.GioiTinh = itemArray[4].ToString().ToLower() == "nam";
                        this.ThemHocSinh(entity);
                    }
                }
                //update lai si so lop
                var siSoLop = this.GetSiSoLop(currentLopHoc);
                this.CapNhatSiSoLop(currentLopHoc, siSoLop);                
            }
            return true;
        }

        public HocSinh GetHocSinhByLopHocAndSoThuTu(int lopHocId, int soThuTu)
        {
            return this._bsEntities.HocSinh.SingleOrDefault(x => x.LopHocId == lopHocId && x.SoThuTu == soThuTu);
        }

        public IList<HocSinh> GetListOfHocSinhByLopHoc(int lopHocId)
        {
            return this._bsEntities.HocSinh.Where(x => x.LopHocId == lopHocId).ToList();
        }

        public bool CheckExistHocSinh(int lopHocId, int soThuTu)
        {
            var hocSinh = this._bsEntities.HocSinh.SingleOrDefault(x => x.LopHocId == lopHocId && x.SoThuTu == soThuTu);
            return hocSinh != null;
        }
        #endregion
    }
}
